package com.richfit.barcodesystem_cczt.fragments.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.richfit.barcodesystem_cczt.R;
import com.richfit.barcodesystem_cczt.activitis.Detail;
import com.richfit.barcodesystem_cczt.databinding.LayoutFragmentDetailBinding;
import com.richfit.barcodesystem_cczt.fragments.model.ZBoxEntity;

import java.util.List;

/**
 * @author cl 20211117
 */
public class BatchCheckOut_Detail_Adapter extends RecyclerView.Adapter<BatchCheckOut_Detail_Adapter.ViewHolder> {
    private static final String TAG = "Detail_Adapter";
    private Context context;
    private List<ZBoxEntity.Line> list;
    /**
     * 记录最后的位置
     */
    private int lastX = 0;
    private int initX = 0;
    /**
     * 利用接口 -> 给RecyclerView设置点击事件
     */
    private ItemClickListener mItemClickListener;
    private ItemRemoveListener mitemRemoveListener;

    public BatchCheckOut_Detail_Adapter(Context context, List<ZBoxEntity.Line> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

    public void setOnItemRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.mitemRemoveListener = itemRemoveListener;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutFragmentDetailBinding binding = LayoutFragmentDetailBinding.inflate(LayoutInflater.from(this.context));
        return new ViewHolder(binding.getRoot());
    }

    @SuppressLint({"ResourceAsColor", "ClickableViewAccessibility"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        LayoutFragmentDetailBinding binding = LayoutFragmentDetailBinding.bind(holder.itemView);
        ZBoxEntity.Line line = getItem(position);
        binding.TxtBoxNo.setText(line.getZboxNum() + line.getBoxNo());
        binding.TxtPackingSpec.setText(line.getPackingSpec());
        binding.TxtLength.setText(line.getsHeight());
        binding.TxtWidth.setText(line.getsWidth());
        binding.TxtHeight.setText(line.getsHeight());
        binding.TxtVolume.setText(line.getsVolume());
        binding.TxtNetWeight.setText(line.getsNetWeight());
        binding.TxtGrossWeight.setText(line.getsGrossWeight());
        binding.TxtLocation.setText(line.getLocation());
        binding.TxtDetail.setTag(line.getZboxLineId());

        if (getItem(position).getIsChecked()) {
            binding.LLItem.setBackgroundResource(R.drawable.shape_item_radius_green);
            binding.CbChoose.setImageResource(R.mipmap.cb_yes);
        } else {
            binding.LLItem.setBackgroundResource(R.drawable.shape_item_radius);
            binding.CbChoose.setImageResource(R.mipmap.cb_no);
        }
        //是否能执行点击事件
        final Boolean[] isClick = {true};
        // 点击事件一般都写在绑定数据这里，当然写到上边的创建布局时候也是可以的
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(v -> {
//                if (isClick[0]) {
//                    // 这里利用回调来给RecyclerView设置点击事件
//                    mItemClickListener.onItemClick(position, getItem(position));
//                }
            });
        }
        binding.TxtDetail.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(context, Detail.class);
            intent.putExtra("boxId", v.getTag().toString());
            intent.putExtra("boxNo", binding.TxtBoxNo.getText().toString());
            context.startActivity(intent);
        });
        final Boolean[] isStop = {false};
        holder.itemView.setOnTouchListener((v, event) -> {
//            Log.i(TAG, "onBindViewHolder: " + isStop[0]);
            if (isStop[0]) {
//                if (mitemRemoveListener != null) {
//                    mitemRemoveListener.onItemRemove(position, getItem(position));
//                }
                return false;
            }
            switch (event.getAction()) {
                case (MotionEvent.ACTION_DOWN):
                    //按下按钮，记录开始移动标记
                    lastX = (int) event.getRawX();
                    initX = (int) event.getRawX();
//                    Log.i(TAG, "onBindViewHolder: ACTION_DOWN");
                    break;
                case MotionEvent.ACTION_MOVE:
                    isClick[0] = false;
                    int x = (int) (event.getRawX() - lastX);
                    if (x < 0) {
                        int y = 0;// (int) (event.getRawY() - lastY);
                        int l = v.getLeft() + x;
                        int r = v.getRight() + x;
                        int t = v.getTop() + y;
                        int b = v.getBottom() + y;
                        v.layout(l, t, r, b);
                        lastX = (int) event.getRawX();
                        if (Math.abs(initX - lastX) > v.getWidth() * 2 / 3) {
                            isStop[0] = true;
                            if (mitemRemoveListener != null) {
                                mitemRemoveListener.onItemRemove(position, getItem(position));
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (isClick[0]) {
                        if (mItemClickListener != null) {
                            // 这里利用回调来给RecyclerView设置点击事件
                            mItemClickListener.onItemClick(position, getItem(position));
                        }
                    } else {
                        isClick[0] = true;
                        this.notifyDataSetChanged();
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:
                    isClick[0] = true;
                    this.notifyDataSetChanged();
                    break;
                default:
                    break;
            }
            return false;
        });
    }

    public ZBoxEntity.Line getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface ItemClickListener {
        void onItemClick(int position, ZBoxEntity.Line line);
    }

    public interface ItemRemoveListener {
        void onItemRemove(int position, ZBoxEntity.Line line);
    }


    /**
     * ViewHolder类将子项布局中所有控件绑定为一个对象，该对象包含子项布局的所有控件
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

}
